import { Component, OnDestroy, OnInit } from "@angular/core";
import { LocalStorageService } from "../local-storage-service";
import { SharedService } from "../shared-service";

declare var $: any;

@Component({
  selector: "hkscc",
  template: `
    <div class="rtop">
      <form>
        <span class="selectlabel2">场次：</span>
        <select *ngIf="ksccArr.length" id="ksccInner" name="kscc" [(ngModel)]="nowccId" (change)="ksccChange()">
          <option *ngFor="let  item of ksccArr" [value]="item.ccm">
            {{item.ccmc}}
          </option>
        </select>
      </form>
    </div>
  `,
  styles: [
    '.rtop{padding: 10px 0px}',
    '.selectlabel2{padding-left: 20px;color: #FCF9F2;font-weight: bolder;}',
    'select::-ms-expand{display: none;}'
  ]
})
export class PublicCompKscc implements OnInit, OnDestroy {
  ksccArr: any[] = [];
  nowccId: string = "";
  nowccmc: string = "";

  initkscc(): void {
    const ksccliststr = this.localstorage.get("hcclist");
    if (ksccliststr) {
      const kscclist = JSON.parse(ksccliststr);
      if (kscclist && kscclist.length) {
        this.ksccArr = kscclist;
        this.nowccId = this.localstorage.get("hkscc");
        this.nowccmc = unescape(this.localstorage.get("hksccmc"));
      }
    }
  }

  /**
   * 考试场次变化
   */
  ksccChange(): void {
    this.localstorage.set("hkscc", this.nowccId);
    this.nowccmc = $("#ksccInner  option:selected").text().toString().trim();
    this.localstorage.set("hksccmc", escape(this.nowccmc));
    this.shareservice.emitChange({
      actiontype: "ksccchange",
      frompage: "hkscc"
    });
  }

  constructor(private localstorage: LocalStorageService,
    private shareservice: SharedService) {

  }

  ngOnInit() {
    this.initkscc();
  }

  ngOnDestroy() {

  }

}
